﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:T_Function
	/// </summary>
	public partial class T_Function
	{
		public T_Function()
		{}
		#region  BasicMethod



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(Model.T_Function model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_Function(");
            strSql.Append("FunID,FunName,FunDescribe,IsUse,ParentFunID,LevelID,FrmName,FrmCode,IcoName,IcoAddr,CreateDateTime,Abbreviation)");
			strSql.Append(" values (");
            strSql.Append("@FunID,@FunName,@FunDescribe,@IsUse,@ParentFunID,@LevelID,@FrmName,@FrmCode,@IcoName,@IcoAddr,@CreateDateTime,@Abbreviation)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@FunID", SqlDbType.NVarChar,10),
					new SqlParameter("@FunName", SqlDbType.NVarChar,20),
					new SqlParameter("@FunDescribe", SqlDbType.NVarChar,50),
					new SqlParameter("@IsUse", SqlDbType.Real,4),
					new SqlParameter("@ParentFunID", SqlDbType.NVarChar,10),
					new SqlParameter("@LevelID", SqlDbType.Int,4),
					new SqlParameter("@FrmName", SqlDbType.NVarChar,20),
					new SqlParameter("@FrmCode", SqlDbType.NVarChar,20),
					new SqlParameter("@IcoName", SqlDbType.NVarChar,10),
					new SqlParameter("@IcoAddr", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateDateTime", SqlDbType.DateTime),
					new SqlParameter("@Abbreviation", SqlDbType.NVarChar,10)};
			parameters[0].Value = model.FunID;
			parameters[1].Value = model.FunName;
			parameters[2].Value = model.FunDescribe;
			parameters[3].Value = model.IsUse;
			parameters[4].Value = model.ParentFunID;
			parameters[5].Value = model.LevelID;
			parameters[6].Value = model.FrmName;
			parameters[7].Value = model.FrmCode;
			parameters[8].Value = model.IcoName;
			parameters[9].Value = model.IcoAddr;
            parameters[10].Value = model.CreateDateTime;
            parameters[11].Value = model.Abbreviation;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.T_Function model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_Function set ");
			strSql.Append("FunID=@FunID,");
			strSql.Append("FunName=@FunName,");
			strSql.Append("FunDescribe=@FunDescribe,");
			strSql.Append("IsUse=@IsUse,");
			strSql.Append("ParentFunID=@ParentFunID,");
			strSql.Append("LevelID=@LevelID,");
			strSql.Append("FrmName=@FrmName,");
			strSql.Append("FrmCode=@FrmCode,");
			strSql.Append("IcoName=@IcoName,");
			strSql.Append("IcoAddr=@IcoAddr,");
            strSql.Append("CreateDateTime=@CreateDateTime,");
            strSql.Append("Abbreviation=@Abbreviation");
			strSql.Append(" where FID=@FID");
			SqlParameter[] parameters = {
					new SqlParameter("@FunID", SqlDbType.NVarChar,10),
					new SqlParameter("@FunName", SqlDbType.NVarChar,20),
					new SqlParameter("@FunDescribe", SqlDbType.NVarChar,50),
					new SqlParameter("@IsUse", SqlDbType.Real,4),
					new SqlParameter("@ParentFunID", SqlDbType.NVarChar,10),
					new SqlParameter("@LevelID", SqlDbType.Int,4),
					new SqlParameter("@FrmName", SqlDbType.NVarChar,20),
					new SqlParameter("@FrmCode", SqlDbType.NVarChar,20),
					new SqlParameter("@IcoName", SqlDbType.NVarChar,10),
					new SqlParameter("@IcoAddr", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateDateTime", SqlDbType.DateTime),
					new SqlParameter("@FID", SqlDbType.BigInt,8),
					new SqlParameter("@Abbreviation", SqlDbType.NVarChar,10)};
			parameters[0].Value = model.FunID;
			parameters[1].Value = model.FunName;
			parameters[2].Value = model.FunDescribe;
			parameters[3].Value = model.IsUse;
			parameters[4].Value = model.ParentFunID;
			parameters[5].Value = model.LevelID;
			parameters[6].Value = model.FrmName;
			parameters[7].Value = model.FrmCode;
			parameters[8].Value = model.IcoName;
			parameters[9].Value = model.IcoAddr;
			parameters[10].Value = model.CreateDateTime;
            parameters[11].Value = model.FID;
            parameters[12].Value = model.Abbreviation;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long FID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Function ");
			strSql.Append(" where FID=@FID");
			SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.BigInt)
			};
			parameters[0].Value = FID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string FIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Function ");
			strSql.Append(" where FID in ("+FIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.T_Function GetModel(long FID)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 FID,FunID,FunName,FunDescribe,IsUse,ParentFunID,LevelID,FrmName,FrmCode,IcoName,IcoAddr,CreateDateTime,Abbreviation from T_Function ");
			strSql.Append(" where FID=@FID");
			SqlParameter[] parameters = {
					new SqlParameter("@FID", SqlDbType.BigInt)
			};
			parameters[0].Value = FID;

			Model.T_Function model=new Model.T_Function();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.T_Function DataRowToModel(DataRow row)
		{
			Model.T_Function model=new Model.T_Function();
			if (row != null)
			{
				if(row["FID"]!=null && row["FID"].ToString()!="")
				{
					model.FID=long.Parse(row["FID"].ToString());
				}
				if(row["FunID"]!=null)
				{
					model.FunID=row["FunID"].ToString();
				}
				if(row["FunName"]!=null)
				{
					model.FunName=row["FunName"].ToString();
				}
				if(row["FunDescribe"]!=null)
				{
					model.FunDescribe=row["FunDescribe"].ToString();
				}
				if(row["IsUse"]!=null && row["IsUse"].ToString()!="")
				{
					model.IsUse=decimal.Parse(row["IsUse"].ToString());
				}
				if(row["ParentFunID"]!=null)
				{
					model.ParentFunID=row["ParentFunID"].ToString();
				}
				if(row["LevelID"]!=null && row["LevelID"].ToString()!="")
				{
					model.LevelID=int.Parse(row["LevelID"].ToString());
				}
				if(row["FrmName"]!=null)
				{
					model.FrmName=row["FrmName"].ToString();
				}
				if(row["FrmCode"]!=null)
				{
					model.FrmCode=row["FrmCode"].ToString();
				}
				if(row["IcoName"]!=null)
				{
					model.IcoName=row["IcoName"].ToString();
				}
				if(row["IcoAddr"]!=null)
				{
					model.IcoAddr=row["IcoAddr"].ToString();
				}
				if(row["CreateDateTime"]!=null && row["CreateDateTime"].ToString()!="")
				{
					model.CreateDateTime=DateTime.Parse(row["CreateDateTime"].ToString());
                }
                if (row["Abbreviation"] != null && row["Abbreviation"].ToString() != "")
                {
                    model.Abbreviation = row["Abbreviation"].ToString();
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select FID,FunID,FunName,FunDescribe,IsUse,ParentFunID,LevelID,FrmName,FrmCode,IcoName,IcoAddr,CreateDateTime,Abbreviation ");
			strSql.Append(" FROM T_Function ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" FID,FunID,FunName,FunDescribe,IsUse,ParentFunID,LevelID,FrmName,FrmCode,IcoName,IcoAddr,CreateDateTime,Abbreviation ");
			strSql.Append(" FROM T_Function ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM T_Function ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.FID desc");
			}
			strSql.Append(")AS Row, T.*  from T_Function T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "T_Function";
			parameters[1].Value = "FID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

